home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 2002 November / SGI Freeware 2002 November - Disc 3.iso / dist / fw_qt3.idb / usr / freeware / Qt / tutorial / t13 / gamebrd.cpp.z / gamebrd.cpp
C/C++ Source or Header  |  2002-04-08  |  3KB  |  130 lines

  1. /****************************************************************
  2. **
  3. ** Implementation of GameBoard class, Qt tutorial 13
  4. **
  5. ****************************************************************/
  6.  
  7. #include "gamebrd.h"
  8.  
  9. #include <qfont.h>
  10. #include <qapplication.h>
  11. #include <qlabel.h>
  12. #include <qpushbutton.h>
  13. #include <qlcdnumber.h>
  14. #include <qlayout.h>
  15.  
  16. #include "lcdrange.h"
  17. #include "cannon.h"
  18.  
  19. GameBoard::GameBoard( QWidget *parent, const char *name )
  20.         : QWidget( parent, name )
  21. {
  22.     QPushButton *quit = new QPushButton( "&Quit", this, "quit" );
  23.     quit->setFont( QFont( "Times", 18, QFont::Bold ) );
  24.  
  25.     connect( quit, SIGNAL(clicked()), qApp, SLOT(quit()) );
  26.  
  27.     LCDRange *angle  = new LCDRange( "ANGLE", this, "angle" );
  28.     angle->setRange( 5, 70 );
  29.  
  30.     LCDRange *force  = new LCDRange( "FORCE", this, "force" );
  31.     force->setRange( 10, 50 );
  32.  
  33.     cannonField = new CannonField( this, "cannonField" );
  34.  
  35.     connect( angle, SIGNAL(valueChanged(int)),
  36.          cannonField, SLOT(setAngle(int)) );
  37.     connect( cannonField, SIGNAL(angleChanged(int)),
  38.          angle, SLOT(setValue(int)) );
  39.  
  40.     connect( force, SIGNAL(valueChanged(int)),
  41.          cannonField, SLOT(setForce(int)) );
  42.     connect( cannonField, SIGNAL(forceChanged(int)),
  43.          force, SLOT(setValue(int)) );
  44.  
  45.     connect( cannonField, SIGNAL(hit()),
  46.          this, SLOT(hit()) );
  47.     connect( cannonField, SIGNAL(missed()),
  48.          this, SLOT(missed()) );
  49.  
  50.     QPushButton *shoot = new QPushButton( "&Shoot", this, "shoot" );
  51.     shoot->setFont( QFont( "Times", 18, QFont::Bold ) );
  52.  
  53.     connect( shoot, SIGNAL(clicked()), SLOT(fire()) );
  54.     connect( cannonField, SIGNAL(canShoot(bool)),
  55.          shoot, SLOT(setEnabled(bool)) );
  56.  
  57.     QPushButton *restart 
  58.     = new QPushButton( "&New Game", this, "newgame" );
  59.     restart->setFont( QFont( "Times", 18, QFont::Bold ) );
  60.  
  61.     connect( restart, SIGNAL(clicked()), this, SLOT(newGame()) );
  62.  
  63.     hits = new QLCDNumber( 2, this, "hits" );
  64.     shotsLeft = new QLCDNumber( 2, this, "shotsleft" );
  65.     QLabel *hitsL = new QLabel( "HITS", this, "hitsLabel" );
  66.     QLabel *shotsLeftL 
  67.     = new QLabel( "SHOTS LEFT", this, "shotsleftLabel" );
  68.  
  69.     QGridLayout *grid = new QGridLayout( this, 2, 2, 10 );
  70.     grid->addWidget( quit, 0, 0 );
  71.     grid->addWidget( cannonField, 1, 1 );
  72.     grid->setColStretch( 1, 10 );
  73.  
  74.     QVBoxLayout *leftBox = new QVBoxLayout;
  75.     grid->addLayout( leftBox, 1, 0 );
  76.     leftBox->addWidget( angle );
  77.     leftBox->addWidget( force );
  78.  
  79.     QHBoxLayout *topBox = new QHBoxLayout;
  80.     grid->addLayout( topBox, 0, 1 );
  81.     topBox->addWidget( shoot );
  82.     topBox->addWidget( hits );
  83.     topBox->addWidget( hitsL );
  84.     topBox->addWidget( shotsLeft );
  85.     topBox->addWidget( shotsLeftL );
  86.     topBox->addStretch( 1 );
  87.     topBox->addWidget( restart );
  88.  
  89.     angle->setValue( 60 );
  90.     force->setValue( 25 );
  91.     angle->setFocus();
  92.  
  93.     newGame();
  94. }
  95.  
  96.  
  97. void GameBoard::fire()
  98. {
  99.     if ( cannonField->gameOver() || cannonField->isShooting() )
  100.     return;
  101.     shotsLeft->display( shotsLeft->intValue() - 1 );
  102.     cannonField->shoot();
  103. }
  104.  
  105.  
  106. void GameBoard::hit()
  107. {
  108.     hits->display( hits->intValue() + 1 );
  109.     if ( shotsLeft->intValue() == 0 )
  110.     cannonField->setGameOver();
  111.     else
  112.     cannonField->newTarget();
  113. }
  114.  
  115.  
  116. void GameBoard::missed()
  117. {
  118.     if ( shotsLeft->intValue() == 0 )
  119.     cannonField->setGameOver();
  120. }
  121.  
  122.  
  123. void GameBoard::newGame()
  124. {
  125.     shotsLeft->display( 15 );
  126.     hits->display( 0 );
  127.     cannonField->restartGame();
  128.     cannonField->newTarget();
  129. }
  130.